package com.spc.car.web.controller.bond;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.constant.CommConstants;
import com.spc.car.data.domain.bond.BiddingBondCollectVO;
import com.spc.car.data.domain.bond.BiddingBondReturnVO;
import com.spc.car.data.domain.bond.BondConfirmVO;
import com.spc.car.data.domain.project.dto.BiddingBondCollectDTO;
import com.spc.car.data.domain.project.dto.BiddingBondListDTO;
import com.spc.car.data.domain.project.dto.BiddingBondReturnDTO;
import com.spc.car.data.entity.project.ProjectBiddingBond;
import com.spc.car.data.service.project.ProjectBiddingBondService;
import com.spc.car.data.service.project.ProjectBiddingEnteredService;
import com.spc.car.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Api(tags = "保证金收取/退还")
@Validated
@Slf4j
@RestController
@RequestMapping("/bond")
public class BiddingBondController extends BaseController {

    @Autowired
    ProjectBiddingEnteredService biddingEnteredService;

    @Autowired
    ProjectBiddingBondService biddingBondService;

    @ApiOperation(value = "获取投标报名登记信息列表", notes = "获取投标报名登记信息列表")
    @PostMapping("/listBiddingEntered")
    public AjaxResult listBiddingEntered(@RequestParam(name = "projectCode", required = true) String projectCode,
                                         @RequestParam(name = "sectionCode", required = true) String sectionCode) {
        List<BiddingBondListDTO> list =
                biddingEnteredService.listProjectBiddingBond(projectCode, sectionCode);

        return AjaxResult.success("查询成功！",list);
    }

    @ApiOperation(value = "保证金收取", notes = "保证金收取保存")
    @PostMapping("/saveBondCollect")
    public AjaxResult saveBondCollect(@Valid @RequestBody BiddingBondCollectVO bondCollectVO) {
        biddingBondService.biddingBondCollect(bondCollectVO, getPrintUser());
        return AjaxResult.success("保证金收取成功!");
    }

    @ApiOperation(value = "保证金退还", notes = "保证金退还保存")
    @PostMapping("/saveBondReturn")
    public AjaxResult saveBondReturn(@Valid @RequestBody BiddingBondReturnVO bondReturnRecordVO) {
        biddingBondService.biddingBondReturn(bondReturnRecordVO, getPrintUser());
        return AjaxResult.success("保证金退还成功!");
    }

    @ApiOperation(value = "保证金收取/退还列表", notes = "通过类型传指定值：C-已收取;R-已退还;WC-待收取;WR-待退还")
    @PostMapping("/listBond")
    public AjaxResult listBond(@Pattern(regexp = "C|WC|R", message = "输入类型有误！")
                               @RequestParam(name = "showType", required = true) String showType,
                               @RequestParam(name = "projectCode", required = true) String projectCode,
                               @RequestParam(name = "sectionCode", required = true) String sectionCode) {

        if (Objects.equals(CommConstants.BondConstants.BOND_COLLECT, showType)) {
            List<BiddingBondCollectDTO> bondList = biddingBondService.listBiddingBondCollect(projectCode, sectionCode, showType);
            return AjaxResult.success("查询成功！", bondList);
        }

        if (Objects.equals(CommConstants.BondConstants.BOND_RETURN, showType)) {
            List<BiddingBondReturnDTO> list = biddingBondService.listBiddingBondReturn(projectCode, sectionCode, showType);
            return AjaxResult.success("查询成功！", list);
        }
        return AjaxResult.error();
    }

    @ApiOperation(value = "确认保证金收取/退还", notes = "确认保证金收取/退还(C-收取；R-退还)")
    @PostMapping("/confirm")
    public AjaxResult confirm(@Valid @RequestBody BondConfirmVO bondConfirmVO) {

        ProjectBiddingBond biddingBond =
                biddingBondService.getOne(new LambdaQueryWrapper<ProjectBiddingBond>()
                        .eq(ProjectBiddingBond::getProjectCode, bondConfirmVO.getProjectCode())
                        .eq(ProjectBiddingBond::getSectionCode, bondConfirmVO.getSectionCod())
                        .eq(ProjectBiddingBond::getOrgCode, bondConfirmVO.getOrgCode()));

        if (Objects.equals(CommConstants.BondConstants.BOND_COLLECT, bondConfirmVO.getStatus())) {
            biddingBond.setCollectStatus(CommConstants.BondConstants.BOND_COLLECT);
            biddingBond.setCollectTime(new Date());
            biddingBondService.saveOrUpdate(biddingBond);
            return AjaxResult.success("确认成功！");
        }

        /*if (Objects.equals(CommConstants.BondConstants.BOND_RETURN, bondConfirmVO.getStatus())) {
            biddingBond.setReturnStatus(CommConstants.BondConstants.BOND_RETURN);
            biddingBond.setReturnTime(new Date());
        }*/
        return AjaxResult.error();
    }
}
